<template>
  <van-button
    v-if="userFollowed"
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="followLoading"
    >已关注</van-button
  >
  <van-button
    @click="onFollow"
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="followLoading"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: '',
  components: {},
  props: {
    userFollowed: {
      type: Boolean,
      required: true
    },
    followUserid: {
      type: [String, Number],
      required: true
    }
  },
  //   修改 props 的 value 名字
  model: {
    prop: 'userFollowed', // 修改prop属性名
    event: 'changeFollowed' // 修改 event 事件名字
  },
  data () {
    return {
      followLoading: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 用户关注/取消关注
    async onFollow () {
      this.followLoading = true

      if (!this.$store.state.user) return this.$toast('请登录')
      // 发送请求实现关注
      try {
        if (this.userFollowed) {
          await deleteFollow(this.followUserid)
        } else {
          await addFollow(this.followUserid)
        }
        // this.isfollowed = !this.isfollowed;
        this.$emit('changeFollowed', !this.userFollowed)
      } catch (error) {
        if (error && error.response.status === 400) {
          this.$toast('不能关注自己')
        } else {
          this.$toast.fail('操作失败')
        }
      }
      this.followLoading = false
    }
  }
}
</script>
<style lang="less" scoped></style>
